package com.mstore.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.mstore.pojo.Account;
import com.mstore.pojo.BApplication;
import com.mstore.pojo.Balance;
import com.mstore.pojo.BalanceDeleteLog;
import com.mstore.pojo.BaseMember;
import com.mstore.pojo.Consume;
import com.mstore.pojo.ConsumeDeleteLog;
import com.mstore.pojo.EnterpriseIntegralRule;
import com.mstore.pojo.Member;
import com.mstore.pojo.Notice;
import com.mstore.pojo.Score;
import com.mstore.pojo.ScoreDeleteLog;
import com.mstore.pojo.Shop;
import com.mstore.pojo.User;
import com.mstore.service.BApplicationService;
import com.mstore.service.BalanceDeleteLogService;
import com.mstore.service.BalanceService;
import com.mstore.service.BaseActService;
import com.mstore.service.BaseMemberService;
import com.mstore.service.ConsumeDeleteLogService;
import com.mstore.service.ConsumeService;
import com.mstore.service.EnterpriseIntegralRuleService;
import com.mstore.service.MemberService;
import com.mstore.service.NoticeService;
import com.mstore.service.ScoreDeleteLogService;
import com.mstore.service.ScoreService;
import com.mstore.service.ShopService;
import com.mstore.service.UserService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 误操作，退货
 */
public class BApplicationAction extends BaseAction {

	private static final long serialVersionUID = -1540844740270708931L;

	private ConsumeService consumeService = new ConsumeService();

	private Map<String, Object> session = ActionContext.getContext()
			.getSession();

	private Member member = new Member();

	private MemberService memberService = new MemberService();

	private ShopService shopService = new ShopService();

	private Consume consume = new Consume();

	private String beginTime;

	private String endTime;

	private List<Consume> consumeList = new ArrayList<Consume>();

	private UserService userService = new UserService();;

	private String temp;

	private BApplication bApplication = new BApplication();

	private BApplicationService bApplicationService = new BApplicationService();

	private List<BApplication> bList = new ArrayList<BApplication>();

	private BaseActService baseactService = new BaseActService();

	private ScoreService scoreService = new ScoreService();

	private EnterpriseIntegralRuleService enterpriseIntegralRuleService;

	private ConsumeDeleteLogService consumeDeleteLogService;

	private ScoreDeleteLogService scoreDeleteLogService;

	private Balance balance = new Balance();

	private BalanceDeleteLogService balanceDeleteLogService;

	private BalanceService balanceService;

	private List<Balance> balanceList;

	private int index;

	private int page;

	private int line;

	private int attention;

	private String passType;

	private String message;

	private List<Consume> consumes;

	private List<Balance> balances;

	private BaseMemberService baseMemberService;

	private NoticeService noticeService;
	
	private String result;

	private static Logger logger = Logger.getLogger(MemberAction.class
			.getName());

	/**
	 * 根据条件查询
	 * 
	 * @return
	 */

	public String getByCreateTimeAndMemberId() {

		User user = (User) session.get("user");
		if (user != null) {
			Shop shop = new Shop();
			shop.setId(user.getShopId());
			// 获得shop信息
			shop = shopService.getShopById(shop);

			this.member.setEaId(shop.getEaId());
			// 获得查询会员信息
			this.member = (Member) memberService.getOneMember(this.member);
			this.consumeList = new ArrayList<Consume>();
			List<Consume> consumes = new ArrayList<Consume>();
			if (this.member == null && beginTime == null && endTime == null) {
				this.consumeList = new ArrayList<Consume>();
				this.consume = new Consume();
				consume.setShopId(shop.getId());
				consume.setEaId(shop.getEaId());
				line = consumeService.getConsumePage(consume);
				if (line % 14 == 0) {
					page = line / 14;
				}
				else {
					page = (line / 14) + 1;
				}

				if (index < 1) {
					index = 1;
				}
				else if (index > page) {
					index = page;
				}

				consumes = consumeService.getConsumesIndex(index, consume);
				for (int i = 0; i < consumes.size(); i++) {
					Consume c = new Consume();
					c = consumes.get(i);
					BApplication b = new BApplication();
					b.setLogConsumeId(consumes.get(i).getId());
					b = bApplicationService.selectByConsumeId(b);
					if (b != null) {
						c.setApplicationType("1");
					}
					Member member1 = new Member();
					member1.setId(consumes.get(i).getMemberId());
					member1 = memberService.getMemberById(member1);
					c.setMember(member1);

					this.consumeList.add(c);
				}
				return "applicationSelectAllSuccess";
			}

			else if (this.member != null) {
				if (!beginTime.equals("") && !endTime.equals("")) {
					SimpleDateFormat df = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					try {
						Date beginDate = df.parse(beginTime);
						Date endDate = df.parse(endTime);
						if (beginDate.after(endDate)) {

							temp = "getByCreateTimeAndMemberIdSuccess";
							message = "时间范围填写有误";
						}

					}
					catch (Exception e) {
						e.printStackTrace();
					}
				}
				this.consume = new Consume();
				this.consume.setMemberId(this.member.getId());
				this.consume.setShopId(shop.getId());
				line = consumeService.getByCreateTimeAndMemberIdPage(consume,
						beginTime, endTime);
				if (line % 14 == 0) {
					page = line / 14;
				}
				else {
					page = (line / 14) + 1;
				}

				if (index < 1) {
					index = 1;
				}
				else if (index > page) {
					index = page;
				}

				consumes = consumeService.getByCreateTimeAndMemberId(index,
						consume, beginTime, endTime);

				for (int i = 0; i < consumes.size(); i++) {
					Consume c = new Consume();
					c = consumes.get(i);
					BApplication b = new BApplication();
					b.setLogConsumeId(consumes.get(i).getId());
					b = bApplicationService.selectByConsumeId(b);
					if (b != null) {
						c.setApplicationType("1");
					}
					Member member1 = new Member();
					member1.setId(consumes.get(i).getMemberId());
					member1 = memberService.getMemberById(member1);
					c.setMember(member1);
					this.consumeList.add(c);
				}
				temp = "getByCreateTimeAndMemberIdSuccess";
			}
			else

			if (this.member == null && beginTime.equals("")
					&& endTime.equals("")) {
				this.consume = new Consume();
				consume.setShopId(shop.getId());
				consume.setEaId(shop.getEaId());
				line = consumeService.getConsumePage(consume);
				if (line % 14 == 0) {
					page = line / 14;
				}
				else {
					page = (line / 14) + 1;
				}

				if (index < 1) {
					index = 1;
				}
				else if (index > page) {
					index = page;
				}

				consumes = consumeService.getConsumesIndex(index, consume);
				for (int i = 0; i < consumes.size(); i++) {
					Consume c = new Consume();
					c = consumes.get(i);
					BApplication b = new BApplication();
					b.setLogConsumeId(consumes.get(i).getId());
					b = bApplicationService.selectByConsumeId(b);
					if (b != null) {
						c.setApplicationType("1");
					}
					Member member1 = new Member();
					member1.setId(consumes.get(i).getMemberId());
					member1 = memberService.getMemberById(member1);
					c.setMember(member1);

					this.consumeList.add(c);
				}
				temp = "getByCreateTimeAndMemberIdSuccess";
			}
			else if (this.member == null && !beginTime.equals("")
					&& !endTime.equals("")) {
				if (!beginTime.equals("") && !endTime.equals("")) {
					SimpleDateFormat df = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					try {
						Date beginDate = df.parse(beginTime);
						Date endDate = df.parse(endTime);
						if (beginDate.after(endDate)) {

							temp = "getByCreateTimeAndMemberIdSuccess";
							message = "时间范围填写有误";
						}

					}
					catch (Exception e) {
						e.printStackTrace();
					}
				}
				this.consume = new Consume();
				consume.setShopId(shop.getId());
				consume.setEaId(shop.getEaId());
				line = consumeService.getByCreateTimeAndMemberIdPage(consume,
						beginTime, endTime);
				if (line % 14 == 0) {
					page = line / 14;
				}
				else {
					page = (line / 14) + 1;
				}

				if (index < 1) {
					index = 1;
				}
				else if (index > page) {
					index = page;
				}
				consumes = consumeService.getByCreateTimeAndMemberId(index,
						consume, beginTime, endTime);
				for (int i = 0; i < consumes.size(); i++) {
					Consume c = new Consume();
					c = consumes.get(i);
					BApplication b = new BApplication();
					b.setLogConsumeId(consumes.get(i).getId());
					b = bApplicationService.selectByConsumeId(b);
					if (b != null) {
						c.setApplicationType("1");
					}
					Member member1 = new Member();
					member1.setId(consumes.get(i).getMemberId());
					member1 = memberService.getMemberById(member1);
					c.setMember(member1);

					this.consumeList.add(c);
				}
				temp = "getByCreateTimeAndMemberIdSuccess";
			}
		}
		return temp;
	}

	/**
	 * 申请退换
	 * 
	 * @return
	 */

	public String applicationSelect() {

		this.consume = consumeService.getOneById(this.consume);
		Member member1 = new Member();
		member1.setId(this.consume.getMemberId());
		member1 = memberService.getMemberById(member1);
		this.consume.setMember(member1);
		return "applicatonSelectSuccess";

	}

	/**
	 * 消费提交退换申请
	 * 
	 * @return
	 */
	public String submitApplication() {
		try {
			Object obj = session.get("user");
			if (obj == null) {
				return "error";
			}
			User user = (User) obj;

			this.bApplication.setCreateTime(getCreateTime());
			this.bApplication.setLogConsumeId(this.consume.getId());
			Consume con = new Consume();
			con = consumeService.getOneById(this.consume);
			Long eaId = con.getEaId();

			// 当提交申请时进行保存(消费删除备份表)
			ConsumeDeleteLog cLog = new ConsumeDeleteLog();
			cLog.setEaId(eaId);
			cLog.setLogConsumeTime(con.getCreateTime());
			cLog.setMemberId(con.getMemberId());
			cLog.setMoney(con.getMoney());
			cLog.setShopId(con.getShopId());
			cLog.setType(con.getType());
			cLog.setUserId(con.getUserId());

			cLog.setMemberName(con.getMemberName());
			cLog.setShopName(con.getShopName());
			cLog.setUserName(con.getUserName());
			cLog.setEaName(con.getEaName());

			consumeDeleteLogService.save(cLog);

			this.bApplication.setLogConsumeDeleteId(cLog.getId());

			Shop shop = new Shop();
			shop.setId(user.getShopId());
			shop = shopService.getShopById(shop);
			bApplication.setShopId(shop.getId());
			bApplication.setAccountId(shop.getEaId());

			this.bApplication.setUserId(user.getId());
			this.bApplication.setAuditorPass("1");

			bApplicationService.saveApplication(this.bApplication);

			// 保存到消费退换申请通知
			Notice notice = new Notice();
			notice.setEaId(eaId);
			notice.setCreateTime(getCreateTime());
			notice.setType(1);
			notice.setIsRead(0);
			notice.setRelateId(this.bApplication.getId());
			noticeService.addNotices(notice);
		}
		catch (Exception e) {
			logger.error("消费提交退换申请报错", e);
		}

		return "submitApplicationSuccess";
	}

	/**
	 * 审核人查询消费
	 * 
	 * @return
	 */
	public String accountAuditorSelect() {
		try {
			Account account = (Account) session.get("account");
			bList = new ArrayList<BApplication>();

			this.bApplication.setAuditorPass(passType);
			if (account != null) {
				this.bApplication.setAccountId(account.getId());
				List<BApplication> bApplicationList = new ArrayList<BApplication>();
				if ("4".equals(bApplication.getAuditorPass())
						&& beginTime == null && endTime == null) {
					line = bApplicationService.getByAccountIdPage(bApplication);

					if (line % 14 == 0) {
						page = line / 14;
					}
					else {
						page = (line / 14) + 1;
					}

					if (index < 1) {
						index = 1;
					}
					else if (index > page) {
						index = page;
					}
					bApplicationList = bApplicationService.getByAccountId(
							index, bApplication);
					if (bApplicationList.size() != 0) {
						for (int i = 0; i < bApplicationList.size(); i++) {
							BApplication b = new BApplication();
							b = bApplicationList.get(i);
							ConsumeDeleteLog c = new ConsumeDeleteLog();
							if (bApplicationList.get(i).getLogConsumeDeleteId() == null) {
							}
							else {
								c.setId(bApplicationList.get(i)
										.getLogConsumeDeleteId());
								c = consumeDeleteLogService.findById(c);
								Member member1 = new Member();
								member1.setId(c.getMemberId());
								member1 = memberService.getMemberById(member1);
								b.setMember(member1);
								b.setConsumeDeleteLog(c);
								bList.add(b);
							}
						}
					}
				}
				else {
					if (!beginTime.equals("") && !endTime.equals("")) {
						SimpleDateFormat df = new SimpleDateFormat(
								"yyyy-MM-dd HH:mm:ss");
						try {
							Date beginDate = df.parse(beginTime);
							Date endDate = df.parse(endTime);
							if (beginDate.after(endDate)) {

								temp = "accountAuditorSelectSuccess";
								message = "时间范围填写有误";
							}

						}
						catch (Exception e) {
							e.printStackTrace();
						}
					}

					line = bApplicationService.findByAccountIdAndStatuPage(
							bApplication, beginTime, endTime);

					if (line % 14 == 0) {
						page = line / 14;
					}
					else {
						page = (line / 14) + 1;
					}

					if (index < 1) {
						index = 1;
					}
					else if (index > page) {
						index = page;
					}
					bApplicationList = bApplicationService
							.findByAccountIdAndStatu(index, bApplication,
									beginTime, endTime);
					if (bApplicationList.size() != 0) {
						for (int i = 0; i < bApplicationList.size(); i++) {
							BApplication b = new BApplication();
							b = bApplicationList.get(i);
							ConsumeDeleteLog c = new ConsumeDeleteLog();
							if (bApplicationList.get(i).getLogConsumeDeleteId() == null) {
							}
							else {
								c.setId(bApplicationList.get(i)
										.getLogConsumeDeleteId());
								c = consumeDeleteLogService.findById(c);
								Member member1 = new Member();
								member1.setId(c.getMemberId());
								member1 = memberService.getMemberById(member1);
								b.setMember(member1);
								b.setConsumeDeleteLog(c);
								bList.add(b);
							}
						}
					}
				}
			}
		}
		catch (Exception e) {
			logger.error("消费审核查询报错", e);
		}

		return "accountAuditorSelectSuccess";
	}

	/**
	 * 当用户已经审批
	 * 
	 * @return
	 */
	public String submitAuditor() {
		try {
			Account account = (Account) session.get("account");
			this.bApplication.setAuditorId(account.getId());
			this.bApplication.setAuditorTime(getCreateTime());
			this.bApplication.setAuditorPass("2");

			BApplication b = new BApplication();

			b = bApplicationService.getById(this.bApplication);
			Long eaId = b.getAccountId();
			Member member1 = new Member();
			member1.setEaId(eaId);

			Consume c = new Consume();
			Consume con = new Consume();
			c.setId(b.getLogConsumeId());
			con = consumeService.getOneById(c);
			ConsumeDeleteLog conDeleteLog = new ConsumeDeleteLog();

			BaseMember baseMember = new BaseMember();
			baseMember = baseMemberService.getOneBaseMemberByMemberIdAndEaId(
					con.getMemberId(), eaId);

			Score score = new Score();
			score.setCreateTime(con.getCreateTime());
			score.setMemberId(con.getMemberId());
			score = scoreService.getByTimeAndMember(score);

			ScoreDeleteLog s = new ScoreDeleteLog();
			s.setCreateTime(getCreateTime());
			s.setEaId(score.getEaId());
			s.setLogScoreTime(score.getCreateTime());
			s.setMemberId(score.getMemberId());
			s.setNumer(score.getNumer());
			s.setShopId(score.getShopId());
			s.setType(score.getType());
			s.setUserId(score.getUserId());
			s.setMemberName(score.getMemberName());
			s.setShopName(score.getShopName());
			s.setUserName(score.getUserName());
			s.setEaName(score.getEaName());
			scoreDeleteLogService.save(s);

			conDeleteLog.setCreateTime(getCreateTime());
			conDeleteLog.setLogScoreDeleteId(s.getId());
			conDeleteLog.setId(b.getLogConsumeDeleteId());

			consumeDeleteLogService.updateById(conDeleteLog);

			Double newScore = baseMember.getScore() - score.getNumer();
			// 现金消费
			if (con.getType() == 0) {
				// baseAct.setScore(baseAct.getScore() - score.getNumer());
				// getMemberLevelname(member1, baseAct);
				// baseAct.setLevel(member1.getLevelName());
				// this.baseactService.updateBaseActByScore(baseAct);

				baseMember.setScore(newScore);
				baseMember.setLevel(getMemberLevelname1(eaId, newScore));
				baseMember.setBalance(null); // 不更新余额
				baseMemberService.updateBaseMember(baseMember);

				scoreService.deleteById(score);
				consumeService.deleteById(con);

			}
			// 余额消费
			if (con.getType() == 1) {
				// baseAct.setScore(baseAct.getScore() - score.getNumer());
				// baseAct.setBalance(baseAct.getBalance() + con.getMoney());
				// getMemberLevelname(member1, baseAct);
				// this.baseactService.updateBaseAct(baseAct);

				baseMember.setScore(newScore);
				baseMember.setLevel(getMemberLevelname1(eaId, newScore));
				baseMember.setBalance(baseMember.getBalance() + con.getMoney());
				baseMemberService.updateBaseMember(baseMember);

				scoreService.deleteById(score);
				consumeService.deleteById(con);
			}
			bApplicationService.updateAuditorType(this.bApplication);
		}
		catch (Exception e) {
			logger.error("当用户已经审批报错", e);
		}
		
		return "submitAuditorSuccess";
	}

	/**
	 * 获取积分会员等级
	 */
	private String getMemberLevelname1(Long eaId, Double score) {
		String levelName = "";
		EnterpriseIntegralRule integralRule = new EnterpriseIntegralRule();
		integralRule.setEaId(eaId);
		List<EnterpriseIntegralRule> integralRules = enterpriseIntegralRuleService
				.watchEnterpriseIntegralRule(integralRule);
		for (int i = 0; i < integralRules.size(); i++) {
			if (score.intValue() >= integralRules.get(i).getMinValue()
					&& score < integralRules.get(i).getMaxValue()) {
				levelName = integralRules.get(i).getName();
				break;
			}
			if (i == integralRules.size() - 1) {
				if (score >= integralRules.get(i).getMaxValue()) {
					member.setLevelName(integralRules.get(i).getName());
					levelName = integralRules.get(i).getName();
				}
			}
		}
		return levelName;
	}

	/**
	 * 当用户不同意
	 * 
	 * @return
	 */
	public String submitAuditor1() {
		Account account = (Account) session.get("account");
		this.bApplication.setAuditorId(account.getId());
		this.bApplication.setAuditorTime(getCreateTime());
		this.bApplication.setAuditorPass("3");

		bApplicationService.updateAuditorType(this.bApplication);

		return "submitAuditorSuccess1";
	}

	/**
	 * 分店查询申请记录
	 * 
	 * @return
	 */
	public String auditorRecord() {

		User user = (User) session.get("user");
		if (user != null) {
			Shop shop = new Shop();
			shop.setId(user.getShopId());

			// 获得shop信息
			shop = shopService.getShopById(shop);
			this.bApplication.setShopId(shop.getId());
			this.bApplication.setAccountId(shop.getEaId());
			this.bApplication.setAuditorPass(passType);
			List<BApplication> bApplicationList = new ArrayList<BApplication>();
			if (bApplication.getAuditorPass().equals("4") && beginTime == null
					&& endTime == null) {
				line = bApplicationService.getByShopPage(bApplication);
				if (line % 14 == 0) {
					page = line / 14;
				}
				else {
					page = (line / 14) + 1;
				}

				if (index < 1) {
					index = 1;
				}
				else if (index > page) {
					index = page;
				}
				bApplicationList = bApplicationService.getByShopId(index,
						bApplication);
			}
			else {
				if (!beginTime.equals("") && !endTime.equals("")) {
					SimpleDateFormat df = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					try {
						Date beginDate = df.parse(beginTime);
						Date endDate = df.parse(endTime);
						if (beginDate.after(endDate)) {

							message = "时间范围填写有误";
							return "auditorRecordSuccess";
						}

					}
					catch (Exception e) {
						e.printStackTrace();
					}
				}
				line = bApplicationService.getByShopIdByTimePage(bApplication,
						beginTime, endTime);
				if (line % 14 == 0) {
					page = line / 14;
				}
				else {
					page = (line / 14) + 1;
				}

				if (index < 1) {
					index = 1;
				}
				else if (index > page) {
					index = page;
				}
				bApplicationList = bApplicationService.getByShopIdByTime(index,
						bApplication, beginTime, endTime);
			}
			if (bApplicationList.size() != 0) {
				for (int i = 0; i < bApplicationList.size(); i++) {
					BApplication b = new BApplication();
					b = bApplicationList.get(i);
					ConsumeDeleteLog c = new ConsumeDeleteLog();
					if (bApplicationList.get(i).getLogConsumeDeleteId() == null) {

					}
					else {
						c.setId(bApplicationList.get(i).getLogConsumeDeleteId());
						c = consumeDeleteLogService.findById(c);
						Member member1 = new Member();
						member1.setId(c.getMemberId());
						member1 = memberService.getMemberById(member1);
						b.setMember(member1);
						b.setConsumeDeleteLog(c);
						bList.add(b);
					}
				}
			}
		}
		return "auditorRecordSuccess";
	}

	/**
	 * 充值余额误操作
	 * 
	 * @return
	 */
	public String getByCreateTimeAndMemberIdBalacne() {
		Object userObj = session.get("user");
		if (userObj == null) {
			result = "系统异常！";
			return "error";
		}
		User user = (User) userObj;
		
		Shop shop = new Shop();
		shop.setId(user.getShopId());
		// 获得shop信息
		shop = shopService.getShopById(shop);
		if (shop == null) {
			result = "店铺不存在！";
			return "error";
		}

		if (balance == null) {
			balance = new Balance();
		}
		balance.setEaId(shop.getEaId());
		balance.setShopId(shop.getId());
		balance.setType(1); // 仅处理现金支付的
		balance.setMemberMobile(member.getMobile());
		balance.setBeginTime(beginTime);
		balance.setEndTime(endTime);
		balance.setTimeType(0);
		
		// 计算总页数
		line = balanceService.getBalancesByRequireCount(balance);
		if (line % 14 == 0) {
			page = line / 14;
		}
		else {
			page = (line / 14) + 1;
		}

		if (index < 1) {
			index = 1;
		}
		else if (index > page) {
			index = page;
		}
		
		// 获得查询会员信息
		balanceList = balanceService.getBalancesByRequire(balance);
		for (Balance balance : balanceList) {
			int count = bApplicationService.selectByBalaceIdCount(balance.getId());
			if (count != 0) {
				balance.setApplicationType("1");
			}
		}
		return "applicationSelectBalanceAllSuccess";
	}

	/**
	 * 充值误操作申请退
	 * 
	 * @return
	 */
	public String applicationBalance() {

		this.balance = balanceService.selectById(balance);
		Member member1 = new Member();
		member1.setId(this.balance.getMemberId());
		member1 = memberService.getMemberById(member1);
		this.balance.setMember(member1);
		return "applicatonSelectBalanceSuccess";

	}

	/**
	 * 充值误操作提交申请
	 * 
	 * @return
	 */
	public String submitApplicationBalance() {
		Object obj = session.get("user");
		if (obj == null) {
			return "error";
		}
		User user = (User) obj;
		Shop shop = new Shop();
		shop.setId(user.getShopId());
		shop = shopService.getShopById(shop);

		if (shop == null) {
			return "error";
		}
		Long eaId = shop.getEaId();

		this.bApplication.setCreateTime(getCreateTime());
		this.bApplication.setLogBalanceId(balance.getId());
		Balance bal = new Balance();
		bal = balanceService.selectById(balance);
		// 当提交申请时进行保存
		BalanceDeleteLog balLog = new BalanceDeleteLog();
		balLog.setEaId(bal.getEaId());
		balLog.setLogBalanceTime(bal.getCreateTime());
		balLog.setMemberId(bal.getMemberId());
		balLog.setMoney(bal.getMoney());
		balLog.setShopId(bal.getShopId());
		balLog.setUserId(bal.getUserId());
		balLog.setMemberName(bal.getMemberName());
		balLog.setShopName(bal.getShopName());
		balLog.setEaName(bal.getEaName());
		balLog.setUserName(bal.getUserName());
		balanceDeleteLogService.save(balLog);
		this.bApplication.setLogBalanceDeleteId(balLog.getId());

		bApplication.setShopId(shop.getId());
		bApplication.setAccountId(eaId);
		this.bApplication.setUserId(user.getId());
		this.bApplication.setAuditorPass("1");

		BaseMember baseMember = baseMemberService
				.getOneBaseMemberByMemberIdAndEaId(bal.getMemberId(), eaId);
		baseMember.setBalance(baseMember.getBalance() - bal.getMoney());
		// 不更新积分和等级
		baseMember.setLevel(null);
		baseMember.setScore(null);
		baseMemberService.updateBaseMember(baseMember);

		bApplicationService.saveApplication(this.bApplication);

		// 保存到充值退换申请通知
		Notice notice = new Notice();
		notice.setEaId(eaId);
		notice.setCreateTime(getCreateTime());
		notice.setType(2);
		notice.setIsRead(0);
		notice.setRelateId(this.bApplication.getId());
		noticeService.addNotices(notice);

		return "submitApplicationBalanceSuccess";
	}

	/**
	 * 分店充值申请记录
	 * 
	 * @return
	 */
	public String auditorRecordBalance() {
		User user = (User) session.get("user");
		if (user != null) {
			Shop shop = new Shop();
			shop.setId(user.getShopId());

			// 获得shop信息
			shop = shopService.getShopById(shop);

			this.bApplication.setShopId(shop.getId());
			this.bApplication.setAccountId(shop.getEaId());
			this.bApplication.setAuditorPass(passType);

			List<BApplication> bApplicationList = new ArrayList<BApplication>();
			if (bApplication.getAuditorPass().equals("4") && beginTime == null
					&& endTime == null) {
				line = bApplicationService.getByShopPage(bApplication);
				if (line % 14 == 0) {
					page = line / 14;
				}
				else {
					page = (line / 14) + 1;
				}

				if (index < 1) {
					index = 1;
				}
				else if (index > page) {
					index = page;
				}
				bApplicationList = bApplicationService.getByShopId(index,
						bApplication);
			}
			else {
				if (!beginTime.equals("") && !endTime.equals("")) {
					SimpleDateFormat df = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm:ss");
					try {
						Date beginDate = df.parse(beginTime);
						Date endDate = df.parse(endTime);
						if (beginDate.after(endDate)) {

							message = "时间范围填写有误";
							return "auditorRecordBalanceSuccess";
						}

					}
					catch (Exception e) {
						e.printStackTrace();
					}
				}
				line = bApplicationService.getByShopIdByTimePage(bApplication,
						beginTime, endTime);
				if (line % 14 == 0) {
					page = line / 14;
				}
				else {
					page = (line / 14) + 1;
				}

				if (index < 1) {
					index = 1;
				}
				else if (index > page) {
					index = page;
				}
				bApplicationList = bApplicationService.getByShopIdByTime(index,
						bApplication, beginTime, endTime);
			}
			if (bApplicationList.size() != 0) {
				for (int i = 0; i < bApplicationList.size(); i++) {
					BApplication b = new BApplication();
					b = bApplicationList.get(i);
					BalanceDeleteLog balLog = new BalanceDeleteLog();
					if (bApplicationList.get(i).getLogBalanceDeleteId() == null) {

					}
					else {
						balLog.setId(bApplicationList.get(i)
								.getLogBalanceDeleteId());
						balLog = balanceDeleteLogService.findById(balLog);
						Member member1 = new Member();
						member1.setId(balLog.getMemberId());
						member1 = memberService.getMemberById(member1);
						b.setMember(member1);
						b.setBalanceDeleteLog(balLog);
						bList.add(b);
					}
				}

			}
		}
		return "auditorRecordBalanceSuccess";
	}

	/**
	 * 审核人查询充值
	 * 
	 * @return
	 */
	public String accountAuditorSelectBalance() {
		try {
			Account account = (Account) session.get("account");
			bList = new ArrayList<BApplication>();

			this.bApplication.setAuditorPass(passType);
			if (account != null) {
				this.bApplication.setAccountId(account.getId());
				List<BApplication> bApplicationList = new ArrayList<BApplication>();
				if (bApplication.getAuditorPass().equals("4")
						&& beginTime == null && endTime == null) {
					line = bApplicationService.getByAccountIdPage(bApplication);

					if (line % 14 == 0) {
						page = line / 14;
					}
					else {
						page = (line / 14) + 1;
					}

					if (index < 1) {
						index = 1;
					}
					else if (index > page) {
						index = page;
					}
					bApplicationList = bApplicationService.getByAccountId(
							index, bApplication);
					if (bApplicationList.size() != 0) {
						for (int i = 0; i < bApplicationList.size(); i++) {
							BApplication b = new BApplication();
							b = bApplicationList.get(i);
							BalanceDeleteLog balLog = new BalanceDeleteLog();
							if (bApplicationList.get(i).getLogBalanceDeleteId() == null) {
							}
							else {
								balLog.setId(bApplicationList.get(i)
										.getLogBalanceDeleteId());
								balLog = balanceDeleteLogService
										.findById(balLog);
								Member member1 = new Member();
								member1.setId(balLog.getMemberId());
								member1 = memberService.getMemberById(member1);
								b.setMember(member1);
								b.setBalanceDeleteLog(balLog);
								bList.add(b);
							}
						}
					}
				}
				else {
					if (!beginTime.equals("") && !endTime.equals("")) {
						SimpleDateFormat df = new SimpleDateFormat(
								"yyyy-MM-dd HH:mm:ss");
						try {
							Date beginDate = df.parse(beginTime);
							Date endDate = df.parse(endTime);
							if (beginDate.after(endDate)) {

								temp = "accountAuditorSelectBalanceSuccess";
								message = "时间范围填写有误";
							}

						}
						catch (Exception e) {
							e.printStackTrace();
						}
					}
					line = bApplicationService.findByAccountIdAndStatuPage(
							bApplication, beginTime, endTime);

					if (line % 14 == 0) {
						page = line / 14;
					}
					else {
						page = (line / 14) + 1;
					}

					if (index < 1) {
						index = 1;
					}
					else if (index > page) {
						index = page;
					}
					bApplicationList = bApplicationService
							.findByAccountIdAndStatu(index, bApplication,
									beginTime, endTime);
					if (bApplicationList.size() != 0) {
						for (int i = 0; i < bApplicationList.size(); i++) {
							BApplication b = new BApplication();
							b = bApplicationList.get(i);
							BalanceDeleteLog balLog = new BalanceDeleteLog();
							if (bApplicationList.get(i).getLogBalanceDeleteId() == null) {
							}
							else {
								balLog.setId(bApplicationList.get(i)
										.getLogBalanceDeleteId());
								balLog = balanceDeleteLogService
										.findById(balLog);
								Member member1 = new Member();
								member1.setId(balLog.getMemberId());
								member1 = memberService.getMemberById(member1);
								b.setMember(member1);
								b.setBalanceDeleteLog(balLog);
								bList.add(b);
							}
						}
					}
				}
			}
		}
		catch (Exception e) {
			logger.error("充值审核查询", e);
		}

		return "accountAuditorSelectBalanceSuccess";
	}

	/**
	 * 审核充值不同意
	 * 
	 * @return
	 */

	public String submitAuditorBalance1() {
		Object obj = session.get("account");
		if (obj == null) {
			return "error";
		}
		Account account = (Account) obj;
		this.bApplication.setAuditorId(account.getId());
		this.bApplication.setAuditorTime(getCreateTime());
		this.bApplication.setAuditorPass("3");
		BApplication ba = new BApplication();
		ba.setId(this.bApplication.getId());
		ba = bApplicationService.getById(ba);
		balance = new Balance();
		balance.setId(ba.getLogBalanceId());
		balance = balanceService.selectById(balance);

		// BaseAct b=new BaseAct();
		// b.setMemberId(balance.getMemberId());
		// b=baseactService.getOneBaseAct(b);
		// double money=b.getBalance()+balance.getMoney();
		// BaseAct base=new BaseAct();
		// base.setMemberId(b.getMemberId());
		// base.setBalance(money);
		// baseactService.updateBaseActByBalanceByMemberId(base);

		BaseMember baseMember = new BaseMember();
		baseMember = baseMemberService.getOneBaseMemberByMemberIdAndEaId(
				balance.getMemberId(), account.getId());
		baseMember.setBalance(baseMember.getBalance() + balance.getMoney());
		// 不更新积分和等级
		baseMember.setLevel(null);
		baseMember.setScore(null);
		baseMemberService.updateBaseMember(baseMember);

		bApplicationService.updateAuditorType(this.bApplication);

		return "submitAuditorBalanceSuccess1";
	}

	/**
	 * 当用户已经审批
	 * 
	 * @return
	 */
	public String submitAuditorBalance() {
		Account account = (Account) session.get("account");
		this.bApplication.setAuditorId(account.getId());
		this.bApplication.setAuditorTime(getCreateTime());
		this.bApplication.setAuditorPass("2");
		BApplication b = new BApplication();
		b = bApplicationService.getById(this.bApplication);
		Member member1 = new Member();
		member1.setEaId(b.getAccountId());

		Balance bal = new Balance();

		bal.setId(b.getLogBalanceId());
		bal = balanceService.selectById(bal);
		BalanceDeleteLog balLog = new BalanceDeleteLog();

		// BaseAct baseAct = new BaseAct();
		// baseAct.setMemberId(bal.getMemberId());
		// baseAct = baseactService.getOneBaseAct(baseAct);
		balLog.setCreateTime(getCreateTime());

		balLog.setId(b.getLogBalanceDeleteId());
		
		balanceDeleteLogService.updateById(balLog);
		bApplicationService.updateAuditorType(this.bApplication);
		balanceService.deleteById(bal);

		return "submitAuditorBalanceSuccess";
	}

	public BaseActService getBaseactService() {
		return baseactService;
	}

	public void setBaseactService(BaseActService baseactService) {
		this.baseactService = baseactService;
	}

	public ConsumeService getConsumeService() {
		return consumeService;
	}

	public void setConsumeService(ConsumeService consumeService) {
		this.consumeService = consumeService;
	}

	public Member getMember() {
		return member;
	}

	public void setMember(Member member) {
		this.member = member;
	}

	public MemberService getMemberService() {
		return memberService;
	}

	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public ShopService getShopService() {
		return shopService;
	}

	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}

	public Consume getConsume() {
		return consume;
	}

	public void setConsume(Consume consume) {
		this.consume = consume;
	}

	public String getBeginTime() {
		return beginTime;
	}

	public void setBeginTime(String beginTime) {
		this.beginTime = beginTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public List<Consume> getConsumeList() {
		return consumeList;
	}

	public void setConsumeList(List<Consume> consumeList) {
		this.consumeList = consumeList;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getTemp() {
		return temp;
	}

	public void setTemp(String temp) {
		this.temp = temp;
	}

	public BApplication getbApplication() {
		return bApplication;
	}

	public void setbApplication(BApplication bApplication) {
		this.bApplication = bApplication;
	}

	public BApplicationService getbApplicationService() {
		return bApplicationService;
	}

	public void setbApplicationService(BApplicationService bApplicationService) {
		this.bApplicationService = bApplicationService;
	}

	public List<BApplication> getbList() {
		return bList;
	}

	public void setbList(List<BApplication> bList) {
		this.bList = bList;
	}

	public BaseActService getBaseActService() {
		return baseactService;
	}

	public void setBaseActService(BaseActService baseactService) {
		this.baseactService = baseactService;
	}

	public ScoreService getScoreService() {
		return scoreService;
	}

	public void setScoreService(ScoreService scoreService) {
		this.scoreService = scoreService;
	}

	public EnterpriseIntegralRuleService getEnterpriseIntegralRuleService() {
		return enterpriseIntegralRuleService;
	}

	public void setEnterpriseIntegralRuleService(
			EnterpriseIntegralRuleService enterpriseIntegralRuleService) {
		this.enterpriseIntegralRuleService = enterpriseIntegralRuleService;
	}

	public ConsumeDeleteLogService getConsumeDeleteLogService() {
		return consumeDeleteLogService;
	}

	public void setConsumeDeleteLogService(
			ConsumeDeleteLogService consumeDeleteLogService) {
		this.consumeDeleteLogService = consumeDeleteLogService;
	}

	public ScoreDeleteLogService getScoreDeleteLogService() {
		return scoreDeleteLogService;
	}

	public void setScoreDeleteLogService(
			ScoreDeleteLogService scoreDeleteLogService) {
		this.scoreDeleteLogService = scoreDeleteLogService;
	}

	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getLine() {
		return line;
	}

	public void setLine(int line) {
		this.line = line;
	}

	public int getAttention() {
		return attention;
	}

	public void setAttention(int attention) {
		this.attention = attention;
	}

	public String getPassType() {
		return passType;
	}

	public void setPassType(String passType) {
		this.passType = passType;
	}

	public Balance getBalance() {
		return balance;
	}

	public void setBalance(Balance balance) {
		this.balance = balance;
	}

	public BalanceDeleteLogService getBalanceDeleteLogService() {
		return balanceDeleteLogService;
	}

	public void setBalanceDeleteLogService(
			BalanceDeleteLogService balanceDeleteLogService) {
		this.balanceDeleteLogService = balanceDeleteLogService;
	}

	public BalanceService getBalanceService() {
		return balanceService;
	}

	public void setBalanceService(BalanceService balanceService) {
		this.balanceService = balanceService;
	}

	public List<Balance> getBalanceList() {
		return balanceList;
	}

	public void setBalanceList(List<Balance> balanceList) {
		this.balanceList = balanceList;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Consume> getConsumes() {
		return consumes;
	}

	public void setConsumes(List<Consume> consumes) {
		this.consumes = consumes;
	}

	public List<Balance> getBalances() {
		return balances;
	}

	public void setBalances(List<Balance> balances) {
		this.balances = balances;
	}

	public BaseMemberService getBaseMemberService() {
		return baseMemberService;
	}

	public void setBaseMemberService(BaseMemberService baseMemberService) {
		this.baseMemberService = baseMemberService;
	}

	public NoticeService getNoticeService() {
		return noticeService;
	}

	public void setNoticeService(NoticeService noticeService) {
		this.noticeService = noticeService;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

}
